package com.example.test_activity.utils;

import com.example.test_activity.entity.Score;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 获取上学期的成绩
 *
 * Created by 旭 on 2017/3/16/016.
 */

public class GetScore {

    public List<Map<String,String>> getScore(String html) {

        List<Map<String,String>> list = new ArrayList<>();

            Document document = Jsoup.parse(html);
            Elements elements_2 = document.select(".odd");
            if (elements_2.isEmpty())
            {
                return list;
            }

            for (int i=0;i<elements_2.size();++i)
            {
                Score score_1 = new Score();
                int j = 0;
                Element ele = elements_2.get(i);
                for (Element e : ele.getAllElements())
                {
                    if (e.attr("align").equals("center"))
                    {
                        ++j;

                        switch (j)
                        {
                            case 1:
                                //课程号
                                score_1.setcNO(e.text());
                                break;
                            case 2:
                                //课序号
                                score_1.setNum(e.text());
                                break;
                            case 3:
                                //课程名
                                score_1.setName(e.text());
                                break;
                            case 4:
                                //英文名
                                score_1.setName_english(e.text());
                                break;
                            case 5:
                                //学分
                                score_1.setCredit(e.text());
                                break;
                            case 6:
                                //课程属性
                                score_1.setProperties(e.text());
                                break;
                            case 7:
                                //课程最高分
                                score_1.setScore_max(e.text());
                                break;
                            case 8:
                                //课程最低分
                                score_1.setScore_min(e.text());
                                break;
                            case 9:
                                //课程平均分
                                score_1.setScore_average(e.text());
                                break;
                            case 10:
                                //成绩
                                score_1.setScore(e.text());
                                break;
                            case 11:
                                //名次
                                score_1.setRanking(e.text());
                                break;
                            case 12:
                                //未通过原因
                                score_1.setReason(e.text());
                                break;
                        }
                    }
                }
                getData(score_1,list);
            }

        return list;
    }

    private void getData(Score score,List<Map<String,String>> list)
    {
        Map<String,String> map = new HashMap<>();
        map.put("name",score.getName());
        map.put("credit",score.getCredit());
        map.put("score",score.getScore());
        map.put("reason",score.getReason());

        list.add(map);
    }
}
